package aps

import glory.xxd.Utils
import jcifs.smb.SmbFile
import org.apache.commons.logging.LogFactory
import org.json.JSONObject

class WeldingDrawingController {

    /**
     * 焊线图请求文件全名
     * @return
     */

    private final static logger = LogFactory.getLog(this)

    def index() {

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code", "-200")
        standardJSONForPDA.put("message", "")
        standardJSONForPDA.put("data", null)


        def htmlfileName = params.htmlfileName ?: 'test'
        def DiagramVersion = params.DiagramVersion ?: 'test'

        try {
            /*
        * from:成功
        * target:规定文件名必须由：关键字-版本 来查找
        * date:2018-04-18
        * */
            //-88ASB01971L-C
            def seachFileName = String.valueOf("${htmlfileName}-${DiagramVersion}")

            String dir = "G:\\"
            def realFile = Utils.getDirFilePath(dir, htmlfileName, DiagramVersion)
            //println realFile.length()
            def judgeFile = new File(realFile)
            //长度大于3，标识文件存在，不是G:/盘,有文件
            if (realFile.length() > 3) {
                realFile = realFile.replace(dir, "").replace("\\", "/")
                def data = JSONObject.newInstance()
                data.put("retureFileName", realFile)
                standardJSONForPDA.put("code", "200")
                standardJSONForPDA.put("data", data)
            } else {
                standardJSONForPDA.put("message", "服务器无该文件！")
            }
            logger.info("WeldingDrawing realFile:${realFile}")
            logger.info("WeldingDrawing control: seachFileName is ${seachFileName} ,Exists:${judgeFile.exists()} ,isFile:${judgeFile.isFile()}")
        } catch (Exception e) {
            standardJSONForPDA.put("message", "网络异常！")
            e.printStackTrace()
            logger.error(e.getMessage())
        } finally {
            render standardJSONForPDA.toString()
        }
    }

    def testRemoteRedig(){

        def standardJSONForPDA = JSONObject.newInstance()
        standardJSONForPDA.put("code","-200")
        standardJSONForPDA.put("message","")
        standardJSONForPDA.put("data",null)

        //0421212437-D
        def htmlfileName = params.htmlfileName ?: '0421212437'
        def DiagramVersion = params.DiagramVersion ?: 'D'

        try{
            /*
        * from:成功
        * target:规定文件名必须由：关键字-版本 来查找
        * date:2018-04-18
        * */
            def seachFileName = String.valueOf("${htmlfileName}-${DiagramVersion}")

            def file =  Utils.getRemoteShareFiles("smb://172.5.153.99/GlorySoft/ActiveMQ/apache-activemq-5.14.3/lib/","xxd","xxd", seachFileName)

            if(file){
                file = file.getPath()
                def data = JSONObject.newInstance()
                data.put("retureFileName","WeldingDrawing/viewRemoteSource?retureFileName=${file}")
                standardJSONForPDA.put("code","200")
                standardJSONForPDA.put("data",data)
            }else{
                standardJSONForPDA.put("message","网络异常或服务器无该文件！")
            }
            logger.info("WeldingDrawing realFile:${file}")
            logger.info("WeldingDrawing control: seachFileName is ${seachFileName} ,Exists:${file} ")
        }catch (Exception e){
            standardJSONForPDA.put("message","网络异常或服务器无该文件！")
            e.printStackTrace()
            logger.error(e.getMessage())
        }finally{
            render standardJSONForPDA.toString()
        }
    }
    def viewRemoteSource(){

        def retureFileName = params.retureFileName?:""
        if(retureFileName){
            def ext =  retureFileName.substring( retureFileName.lastIndexOf("."), retureFileName.length())

            def file =  Utils.getRemoteShareFiles(retureFileName,"xxd","xxd")

            println retureFileName

            if(!ext.toLowerCase().indexOf("pdf")){
                response.setContentType("application/x-png")
            }else{
                response.setContentType("application/octet-stream");
            }

            def fin = file.getInputStream()
            def out = response.getOutputStream();
            out.write(fin.getBytes())
            out.flush();
            fin.close()
            out.close()
        }
        return;
    }

}